<?php

namespace addons\mall\controller;

use addons\mall\model\Cat as CatModel;

/**
 * 商品分类控制器
 * Class Cat
 * @package addons\mall\controller
 */
class Cat extends Base
{

    public function index()
    {
        $id = $this->request->param('id/d');
        $pagesize = $this->request->param('pagesize/d',16);
        $this->assign('pagesize',$pagesize);
        $cat = CatModel::get($id);

        if (!$cat) {
            $this->error(__('No specified cat found'));
        }

        $filterlist = [];
        $orderlist = [];

        $filter = $this->request->get('filter/a',[]);
        $orderby = $this->request->get('orderby', '');
        $orderway = $this->request->get('orderway', '', 'strtolower');
        $params = ['pagesize' => $pagesize, 'id' => $cat->id];
        if ($filter)
            $params['filter'] = $filter;
        if ($orderby)
            $params['orderby'] = $orderby;
        if ($orderway)
            $params['orderway'] = $orderway;

        $fields = [
            ['name' => 'goods_tag', 'title' => '商品标签', 'content' => ['bestis'=>__("bestis"), 'hotis'=>__("Hotis"), 'newis'=>__("Newis"), 'recomis'=>__("Recomis")]],
        ];
        $filter = array_intersect_key($filter, array_flip(array_column($fields, 'name')));
        foreach ($fields as $v) {
            $content = [];
            $all = ['' => __('All')] + $v['content'];
            foreach ($all as $m => $n) {
                $active = ($m === '' && !isset($filter[$v['name']])) || (isset($filter[$v['name']]) && $filter[$v['name']] == $m) ? TRUE : FALSE;
                $prepare = $m === '' ? array_diff_key($filter, [$v['name'] => $m]) : array_merge($filter, [$v['name'] => $m]);
                $url = '?' . http_build_query(array_merge(['filter' => $prepare], array_diff_key($params, ['filter' => ''])));
                $content[] = ['value' => $m, 'title' => $n, 'active' => $active, 'url' => $url];
            }
            $filterlist[] = [
                'name'    => $v['name'],
                'title'   => $v['title'],
                'content' => $content,
            ];
        }


        $sortrank = [
            ['name' => 'id', 'title' => __('Default')],
            ['name' => 'visitnum', 'title' => __('Visitnum')],
            ['name' => 'salenum', 'title' => __('Salenum')],
            ['name' => 'shopprice', 'title' => __('Shopprice')],
            ['name' => 'appraisenum', 'title' => __('Appraisenum')],
            ['name' => 'saletime', 'title' => __('Saletime')],
        ];
        $orderby = $orderby && in_array($orderby, ['id', 'visitnum', 'salenum', 'shopprice', 'appraisenum', 'saletime']) ? $orderby : 'id';
        $orderway = $orderway ? $orderway : 'desc';
        foreach ($sortrank as $k => $v) {
            $url = '?' . http_build_query(array_merge($params, ['orderby' => $v['name'], 'orderway' => ($orderway == 'desc' ? 'asc' : 'desc')]));
            $v['active'] = $orderby == $v['name'] ? true : false;
            $v['url'] = $url;
            $orderlist[] = $v;
        }

        foreach($filter as $k=>$v){
            if($k=="goods_tag") {
                $filter[$v] = 1;
                unset($filter[$k]);
            }
        }
        $pagelist = \addons\mall\model\Goods::where('status', 'in','sale,warn')
            ->where("","EXP",\think\Db::raw('FIND_IN_SET('.$cat['id'].',goodscat_ids)'))
            ->where($filter)->order($orderby, $orderway)
            ->paginate($pagesize, false, ['type' => '\\addons\\mall\\library\\Bootstrap']);

        //处理关注
        if(cookie('uid')){
            $f = model('\app\common\model\mall\user\Favorite');
            foreach($pagelist as $k=>$v){
                $pagelist[$k]['favShop'] = $f->checkFavorite($v['shop_id'],'shop');
                $pagelist[$k]['favGood'] = $f->checkFavorite($v['id'],'goods');
            }
        }
        $pagelist->appends($params);
        $this->view->assign("__FILTERLIST__", $filterlist);
        $this->view->assign("__ORDERLIST__", $orderlist);
        $this->view->assign("__PAGELIST__", $pagelist);
        $this->view->assign("__CAT__", $cat);
        return $this->view->fetch("/goods/index");
    }

}
